//
//  XPSearchControl.swift
//  PPKit
//
//  Created by xiaopin on 2020/12/24.
//  Copyright © 2020 scoresdata.net. All rights reserved.
//

import UIKit

/// 搜索控件
class XPSearchControl: XPBaseView,UITextFieldDelegate{
    
    var searchClosure:((String)->())?
    //MARK: 属性
    var iconImage = UIImageView.init(name: "search_icon_search", mode: .scaleAspectFit, clips: true)
    var textField = UITextField.init(15, weight: .light, textColor: pp_color(.C222222), placeholder: "请输入搜索关键字", placeholderColor: pp_color(.CCCCCCC), align: .left)
    var btnCancel = UIButton.init(text: "取消", textColor: pp_color(.C159F48), font: .systemFont(ofSize: 15))
    var lineView = UIView.init(bgColor: pp_color(.Seperator))
    
    //MARK: 重载父类方法
    /// 初始化数据
    override func initData() {
        textField.returnKeyType = .search
        textField.delegate = self
    }
    
    /// 初始化当前视图的子控件
    override func initSubViews() {
        iconImage.beAddInto(self).snp.makeConstraints { m in
            m.width.equalTo(22)
            m.height.equalTo(44)
            m.left.equalTo(15.scale)
            m.bottom.equalToSuperview()
        }
        btnCancel.beAddInto(self).snp.makeConstraints { m in
            m.width.height.equalTo(44)
            m.right.equalTo(-15.scale)
            m.bottom.equalToSuperview()
        }
        textField.beAddInto(self).snp.makeConstraints { m in
            m.height.equalTo(44)
            m.left.equalTo(iconImage.snp.right).offset(10.scale)
            m.right.equalTo(btnCancel.snp.left).offset(-10.scale)
            m.bottom.equalToSuperview()
        }
        lineView.beAddInto(self).snp.makeConstraints { m in
            m.height.equalTo(0.5)
            m.left.right.bottom.equalToSuperview()
        }
    }
    
    //MARK: 自定义方法
    
    //MARK: 事件通知
    
    //MARK: 委托代理
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        if let text = textField.text,text.count > 0{
            searchClosure?(text)
        }
        return true
    }
}
